requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    var url = {
        listFirst: "manager/listFirst.do",
        insertFirst: "manager/insertFirst.do",
        updateFirst: "manager/updateFirst.do",
        deleteFirst: "manager/deleteFirst.do",
        ajaxqueryRegion: "manager/ajaxqueryRegion.do",
    }

    var frontData = {
        pageNum: 1
    }

    var products = {};

    //查询订单表
    function queryLender() {
        var data = {
            pageNum: frontData.pageNum,
        }
        site.send({
            url: url.listFirst,
            data: data,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    if (data.content) {
                        $('.lender-page-container').createPager({
                            current: frontData.pageNum,
                            total: data.content.total,
                            pageSize: data.content.pageSize,
                            needInput: false,
                            backFn: function (current) {
                                frontData.pageNum = current;
                                queryLender();
                            }
                        })
                        $('.result').html(data.content.total ? data.content.total : 0);
                    }
                    //handlebars模板引擎
                    var source = $("#parameter_set_content_tpl").html()
                    var template = Handlebars.compile(source)
                    var content = data.content;

                    if (content.list.length) {
                        for (var i = 0; i < content.list.length; i++) {
                            var v = content.list[i];
                            products[v.id] = v;
                        }
                    }

                    var html = template(content)
                    $('#parameter_set_content').html(html)


                    $(".lender_edit").on('click', function () {
                        state = 'update';
                        lender_id = $(this).data('id');
                        var v = products[lender_id];
                        $("#name").val(v.first_party);
                        $("#idCard").val(v.first_party_idcard);

                        $("#province_list").val(v.first_party_province)

                        site.send({
                            url: url.ajaxqueryRegion,
                            data: {
                                parentId: v.first_party_province
                            },
                            callback: function (data) {
                                data = JSON.parse(data);
                                var source = $("#city_list_tpl").html();
                                var template = Handlebars.compile(source);
                                var list = data
                                var html = template(list)
                                $('#city_list').html(html)
                                $("#city_list").val(v.first_party_city)
                            }
                        })

                        site.send({
                            url: url.ajaxqueryRegion,
                            data: {
                                parentId: v.first_party_city
                            },
                            callback: function (data) {
                                data = JSON.parse(data);
                                var source = $("#area_list_tpl").html();
                                var template = Handlebars.compile(source);
                                var list = data
                                var html = template(list)
                                $('#area_list').html(html)
                                $("#area_list").val(v.first_party_area);
                            }
                        })

                        $("#address").val(v.first_party_address);
                        $(".addwinBox .headerSpan").html("编辑出借人");
                        $(".addwinBox").show();
                    })


                    $(".delect_attach").on('click', function () {
                        $(".delete_winBox").toggle();
                        delId = $(this).data('id');
                    })

                    //复选框一个没选中则全选去除
                    $.each($("input[name='ch_id']"), function (i, e) {
                        var flag = false;
                        $(e).on('click', function () {
                            var _checked = $("input[name='ch_id_all']")
                            var _checkedAll = $("input[name='ch_id']")
                            $.each(_checkedAll, function (ii, ee) {
                                if ($(ee).prop('checked')) {
                                    flag = true
                                }
                                else {
                                    flag = false
                                    return false;
                                }
                            })
                            _checked.prop('checked', flag);
                        })
                    })

                    //查询出委托人列表
                    function queryDelegate(current) {
                        site.send({
                            url: url.ChoiceOfclient,
                            data: {
                                pageNum: current
                            },
                            callback: function (data) {
                                data = JSON.parse(data)
                                if (data.type == 1) {
                                    var source = $("#delegate_role_list_tpl").html();
                                    var template = Handlebars.compile(source);
                                    var list = data.content
                                    var html = template(list)
                                    $('.delegate_role_list').html(html)

                                    $('.delegate-role-page-container').createPager({
                                        current: frontData.pageNum,
                                        total: data.content.total,
                                        pageSize: data.content.pageSize,
                                        needInput: false,
                                        backFn: function (current) {
                                            queryDelegate(current);
                                        }
                                    })
                                }
                                else {
                                    Constant.fn.Message("F", data.msg)
                                }
                            }
                        })
                    }


                    //渲染出模板后在进行操作
                    $(".operations").on('change', function () {
                        var choice = $(this).val();
                        product_id = $(this).attr('product_id');
                        order_id = $(this).attr('order_id');
                        taskId = $(this).attr('task_id');
                        node_type = $(this).attr('node_type')
                        node_id = $(this).attr('node_id')
                        s_id = $(this).attr('s_id');
                        if (choice == 'loanBeforeEditOrder') {
                            pjaxGo("loanBefore/orderEdit.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                        }
                        else if (choice == 'checkOrder') {
                            pjaxGo("loanBefore/orderDetail.html?order_id=" + order_id + "&product_id=" + product_id + "&task_id=" + taskId + "&node_id=" + node_id + "&s_id=" + s_id);
                        }
                        else if (choice == 'reviewOrder') {
                            if (node_type !== 3) {
                                $('.formBox .attachment').remove();
                                $('.formBox .productInfos').remove();
                                $('.formBox .fix').remove();
                                $('input[name="faceResult"]').prop('checked', false);
                                $('.role').remove();
                                $('.chooseRole').remove();
                                $('.approveRemark').val('');
                                $('.processForm_winBox').toggle();
                                getLastNodeId(product_id)
                                /*selectAttachmentType(node_id, product_id)*/
                                queryNodeForm(order_id, node_id, product_id)
                            }
                        }
                        else if ($(this).val() == 'orderDelegate') {
                            $('.delegate_winBox').toggle();
                            queryDelegate(1);
                        }
                        else if ($(this).val() == 'backReview') {
                            arr = [{
                                taskId: taskId,
                                orderId: order_id,
                                nodeId: node_id
                            }]
                            $('.backProcess_winBox').toggle();
                        }
                        else if ($(this).val() == 'lookProcess') {
                            $('.processBox').remove()
                            site.send({
                                url: url.getActivitiProccessImage,
                                data: {
                                    taskId: taskId,
                                },
                                callback: function (data) {
                                    data = JSON.parse(data)
                                    if (data.type == 1) {
                                        var source = $("#process_list_tpl").html();
                                        var template = Handlebars.compile(source);
                                        var list = data.content
                                        var html = template(list)
                                        $('.lookProcess').append(html)
                                        $('.lookProcess_winBox').toggle();
                                    }
                                    else {
                                        Constant.fn.Message("F", data.msg);
                                    }
                                }
                            })
                            //queryDelegate(1);
                        }
                        $(this).find("option:first").attr("selected", false).attr("selected", true);
                    });

                    Public.record('t_order_public');

                } else {
                    Constant.fn.Message("F", data.msg);
                }
            },
            callError: function () {
                alert("error")
            }
        })
    }

    queryLender();

    site.send({
        url: url.ajaxqueryRegion,
        data: {
            parentId: 0
        },
        callback: function (data) {
            data = JSON.parse(data);
            var source = $("#province_list_tpl").html();
            var template = Handlebars.compile(source);
            var list = data
            var html = template(list)
            $('#province_list').append(html)
        }
    })

    var add_attach = {
        name: "",
        remarks: "",
        type: ""
    };

    var state, lender_id, delId;

    $(".add_attach").click(function () {
        state = 'add';
        $("#name").val("");
        $("#idCard").val("");
        $("#province_list").val("");
        $("#city_list").html('<option value="" selected="selected">选择市</option>');
        $("#area_list").html('<option value="" selected="selected">选择区县</option>');
        $("#address").val("");
        $(".addwinBox .headerSpan").html("添加出借人");
        $(".addwinBox").show();
        //add_attach.type = $(this).data("type");
        //$(".addAndEditAttach_title").text("新增附件")
        //$(".add_attach_name").val('');
        //$(".add_attach_text").val('');
        //state = "add";
        //event.stopPropagation();
    })

    $("#province_list").on("change", function () {
        if (!$(this).val()) {
            $("#city_list").html('<option value="" selected="selected">选择市</option>');
            $("#area_list").html('<option value="" selected="selected">选择区县</option>');
        }
        else {
            site.send({
                url: url.ajaxqueryRegion,
                data: {
                    parentId: $(this).val()
                },
                callback: function (data) {
                    data = JSON.parse(data);
                    var source = $("#city_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data
                    var html = template(list)
                    $('#city_list').html(html)
                }
            })
        }
    })

    $("#city_list").on("change", function () {
        if (!$(this).val()) {
            $("#area_list").html('<option value="" selected="selected">选择区县</option>');
        } else {
            site.send({
                url: url.ajaxqueryRegion,
                data: {
                    parentId: $(this).val()
                },
                callback: function (data) {
                    data = JSON.parse(data);
                    var source = $("#area_list_tpl").html();
                    var template = Handlebars.compile(source);
                    var list = data
                    var html = template(list)
                    $('#area_list').html(html)
                }
            })
        }
    })

    $("#lender_add").on("click", function () {
        var data = {
            first_party: $("#name").val(),
            first_party_idcard: $("#idCard").val(),
            first_party_address: $('#address').val(),
            first_party_province: $("#province_list").val(),
            first_party_city: $("#city_list").val(),
            first_party_area: $("#area_list").val()
        };
        if (!data.first_party) {
            Constant.fn.Message("I", "请输入姓名")
            return;
        }
        if (!data.first_party_idcard) {
            Constant.fn.Message("I", "请输入身份证")
            return;
        }
        if (!Constant.reg.isCarid.test(data.first_party_idcard)) {
            Constant.fn.Message('I', '身份证号码格式有误！')
            return;
        }
        if (!data.first_party_province) {
            Constant.fn.Message("I", "请选择省份")
            return;
        }
        if (!data.first_party_city) {
            Constant.fn.Message("I", "请选择市")
            return;
        }
        if (!data.first_party_area) {
            Constant.fn.Message("I", "请选择区县")
            return;
        }
        if (!data.first_party_address) {
            Constant.fn.Message("I", "请输入地址")
            return;
        }

        if (state === 'add') {
            site.send({
                url: url.insertFirst,
                data: data,
                callback: function (data) {
                    data = JSON.parse(data);
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);
                        pjaxReload();
                    }
                }
            })
        }
        else {
            data.id = lender_id;
            site.send({
                url: url.updateFirst,
                data: data,
                callback: function (data) {
                    data = JSON.parse(data);
                    if (data.type == 1) {
                        Constant.fn.Message('T', data.msg);
                        pjaxReload();
                    }
                }
            })
        }
    })

    /*删除弹框的操作*/
    $('.sureDel').on('click', function () {
        site.send({
            url: url.deleteFirst,
            data: {
                id: delId
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == 1) {
                    Constant.fn.Message('T', data.msg);
                    pjaxReload();
                }
            }
        })
    })

    //批量操作
    $(".supportc").on('change', function () {
        delId = "";
        var selected = $(this).find("option:selected");
        $(this).find("option:first").prop("selected", false).prop("selected", true);
        $("input[name='ch_id']:checked").each(function (index, item) {
            delId = delId + $(item).val() + ","
        })
        delId = delId.substring(0, delId.length - 1)
        if (selected.val() == '删除') {
            //删除产品
            if (!delId) {
                Constant.fn.Message('I', "未选择要删除的出借人")
                return false;
            }
        }
        $('.delete_winBox').toggle();
    })

    $('.closeBox').on('click',function(){
        $(this).parents('.winBox').toggle();
    })

})